<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 16/4/6
 * Time: 上午11:49
 */
class RoleService extends Service {

    public static function indexHeader(){
        $header = array(
            "accountName"       => array('text' => '账号名称'),
            "roleId"            => array('text' => '角色ID'),
            "roleName"          => array('text' => '角色名称'),
            "bOccupation"       => array('text' => '职业'),
            "iMoney"            => array('text' => '勾玉'),
            "iGold"             => array('text' => '石币'),
            "iConch"            => array('text' => '贝壳'),
            "nLevel"            => array('text' => '等级'),
            "bTransLv"          => array('text' => '转生等级'),
            "bpvpLv"            => array('text' => 'PVP等级'),
            "dwLastLogInTime"   => array('text' => '最后登录时间'),
            "iCreateTime"       => array('text' => '创建角色时间'),
            "online"            => array('text' => '状态'),
        );

        foreach($header as &$v){
            $v['unSort'] = 1;
        }

        return $header;

    }

    public static function indexData($params){
        $args = array(
            'roleName'      => $params['role_name'],
            'roleId'        => $params['role_id'],
            'accountName'   => $params['account_name'],
            'isOnline'      => empty($params['is_online']) ? '0' : $params['is_online'],
            'pageNum'       => $params['page'],
            'pageSize'      => $params['length'],
        );

        $api = new ApiService($params['server']);
        $result = $api->getRoleList($args);

        $roleList = PubDict::dict('roma', 'roleocc');

        foreach($result['data'] as &$v){
            $v['iCreateTime'] = date('Y-m-d H:i:s', $v['iCreateTime']);
            $v['dwLastLogInTime'] = date('Y-m-d H:i:s', $v['dwLastLogInTime']);

            if(isset($roleList[$v['bOccupation']])){
                $v['bOccupation'] = $roleList[$v['bOccupation']];
            }

            $v['online'] = $v['bIsOnline'] ? '<span style="color: green;">在线</span>' : '<span style="color: #999;">下线</span>';

            $v['accountName'] .= ' <a title="查看账号详情" target="_blank" href="'.PubFunc::buildUrl('/oss/?r=account/index', 38)."&server={$params['server']}&search={$v['accountName']}".'"><i class="fa fa-info-circle"></i></a>';
            $v['roleId'] .= ' <a title="查看角色详情" target="_blank" href="'.PubFunc::buildUrl('/oss/?r=role/index', 36)."&server={$params['server']}&search={$v['roleId']}".'"><i class="fa fa-info-circle"></i></a>';
        }
        unset($v);

        return array(
            'result'    => true,
            'count'     => $result['totalNum'],
            'data'      => $result['data'],
        );
    }

    public static function getInfo($server, $search){
        $api = new ApiService($server);
        $result = $api->roleInfoDetails($search);
        if($result['state']){
            $tmp = $result['data'][0];

            $tmp['iCreateTime'] = date('Y-m-d H:i:s', $tmp['iCreateTime']);
            $tmp['dwLastLogInTime'] = date('Y-m-d H:i:s', $tmp['dwLastLogInTime']);

            if($tmp['bIsOnline']){
                $tmp['isOnline'] = '在线';
            }else{
                $tmp['isOnline'] = '不在线';
            }

            $occupationMap = PubDict::dict('roma', 'roleocc');
            if(isset($occupationMap[$tmp['bOccupation']])){
                $tmp['occupation'] = $occupationMap[$tmp['bOccupation']];
            }

            $platMap = array(
                '0' => '无',
                '1' => '海马',
                '2' => 'xy'
            );
            $tmp['plat'] = $platMap[$tmp['iPlatID']];

            return array('state' => true, 'data' => $tmp);
        }else{
            return $result;
        }
    }

    /**
     * 获得用户的扩展属性e
     * @param $roldId
     * @param $type
     */
    public static function getExtra($server, $roleId, $type){
        $api = new ApiService($server);
        $result = $api->roleExtra($roleId, $type);

        // 统一格式化
        if($result['ret'] == 0){
            $data = $result['data'];
            $desc = $result['desc'];

            if($type == 'PropsDetail'){
                $bindMap = array('0' => '未绑定', '1' => '已绑定');
                $stateMap = array('0' => '未开启', '1' => '未镶嵌', '2' => '已镶嵌');
                $positionMap = array('1' => '身上', '2' => '背包', '3' => '仓库');

                foreach($data as &$v){
                    $v['bBind'] = $bindMap[$v['bBind']];
                    $v['iState'] = $stateMap[$v['iState']];
                    $v['position'] = $positionMap[$v['position']];
                }
                unset($v);
            }

            if($type == 'StageDetail'){
                $flagMap = array('0' => '未达标', '1' => '未领取', '2' => '已领取');

                foreach($data as &$v){
                    $v['FlagDesc'] = $flagMap[$v['FlagDesc']];
                }
                unset($v);
            }

            if($type == 'PetDetail'){
                $positionMap = array('1' => '身上', '2' => '背包', '3' => '仓库');

                $dictPetLevel = PubDict::dict('roma', 'petlevel');

                foreach($data as &$v){
                    $v['3position'] = $positionMap[$v['3position']];
                    $v['9chRate']   = $dictPetLevel[$v['9chRate']];
                }
                unset($v);
            }

            return array(
                'state' => $result['state'],
                'data'  => $data,
                'desc'  => $desc
            );
        }else{
            return $result;
        }
    }

}